package com.mpsc.api.unit;

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;

public class linuxUtil {

    public static void main(String[] args) throws IOException {
        String newPath = "/iflytek/sgli";
        String oldPath ="/iflytek/sgli/mv_test/dzmb-v1.0.0.jar";
        linuxMv(oldPath,newPath);
    }

    public static void linuxMv(String oldPath, String newPath) throws IOException {

        final SSHClient ssh = new SSHClient();
        ssh.addHostKeyVerifier(new PromiscuousVerifier());
        ssh.connect("192.168.1.13", 22);
        ssh.authPassword("root", "111111");
        Session session = null;
        try {
            session = ssh.startSession();
            String execute = "mv " + oldPath + " " + newPath;
            final Session.Command cmd = session.exec(execute);
            InputStream inputStream = cmd.getInputStream();
            OutputStream outputStream = IOUtils.readFully(inputStream);
            String str = outputStream.toString();
            System.out.println("-------------" + str);
            cmd.join(5, TimeUnit.SECONDS);
            System.out.println("n** exit status: " + cmd.getExitStatus());
        } finally {
            try {
                if (session != null) {
                    session.close();
                }
            } catch (IOException e) {
                // 处理异常
            }
            ssh.disconnect();
        }

    }
}
